<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="发车日期" prop="departDate">
        <el-date-picker
          v-model="queryParams.departDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="提货日期" prop="pickDate">
        <el-date-picker
          v-model="queryParams.pickDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="线路" prop="transportLine">
        <el-select v-model="queryParams.transportLine" clearable placeholder="请选择线路">
          <el-option
            v-for="item in line"
            :key="item.label"
            :label="item.label"
            :value="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-select
          v-model="queryParams.supplier"
          clearable
          filterable
          placeholder="请选择供应商"
        >
          <el-option
            v-for="item in supplierList"
            :key="item.value"
            :label="item.label"
            :value="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="调度状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择">
          <el-option
            v-for="dict in dict.type.transport_plan_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- 搜索和重置 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 新增、修改、删除 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tms:car:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tms:car:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      :border="true"
      v-loading="loading"
      :data="carList"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column width="50" label="序号" align="center" prop="id" />
      <el-table-column width="160" label="发车日期" align="center" prop="departDate" />
      <el-table-column width="160" label="提货日期" align="center" prop="pickDate" />
      <el-table-column width="120" label="线路" align="center" prop="transportLine" />
      <el-table-column width="120" label="供应商" align="center" prop="supplier">
      </el-table-column>
      <el-table-column width="80" label="物品数" align="center" prop="goodsNum" />
      <el-table-column width="50" label="单位" align="center" prop="goodsUnit" />
      <el-table-column width="250" label="货物尺寸" align="center" prop="goodsSize" />
      <el-table-column width="80" label="吨位" align="center" prop="goodsWeight" />
      <el-table-column width="200" label="备注" align="center" prop="remark" />
      <el-table-column width="160" label="修改时间" align="center" prop="updateTime" />
      <el-table-column width="80" label="操作人" align="center" prop="updateBy" />
      <el-table-column width="90" label="状态" fixed="right" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.transport_plan_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        width="90"
        label="实际进度"
        align="center"
        prop="progress"
      >
        <template slot-scope="scope">
          <el-progress type="circle" :percentage="scope.row.progress"></el-progress>
        </template>
      </el-table-column>
      <el-table-column
        width="250"
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-truck"
            type="text"
            @click="assignPlan(scope.row)"
            >调度
          </el-button>
          <el-button
            size="mini"
            icon="el-icon-view"
            type="text"
            @click="handleDetail(scope.row)"
            >详情
          </el-button>
          <el-button
            size="mini"
            icon="el-icon-edit"
            type="text"
            v-hasPermi="['tms:transportPlan:edit']"
            @click="handleEdit(scope.row)"
            >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['tms:transportPlan:remove']"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <AddPlane
      :dialogVisible="dialogVisible"
      :title="title"
      :line="line"
      @refresh="refresh"
    ></AddPlane>
    <AssignPlan :record="record" @refresh="refresh"></AssignPlan>
    <EditPlan :record="editRecord" :line="line" @refresh="refresh"></EditPlan>
    <PlanDetail :record="detailRecord" @refresh="refresh"></PlanDetail>
  </div>
</template>

<script>
import { deletePlan, getList, listAll } from "@/api/tms/plan";
import { getSupplier } from "@/api/tms/inventory";
import AddPlane from "./components/AddPlan.vue";
import EditPlan from "./components/editPlan.vue";
import AssignPlan from "./components/assignPlan.vue";
import PlanDetail from "./components/planDetail.vue";

export default {
  name: "Car",
  dicts: ["transport_plan_status"],
  data() {
    return {
      supplierList: [],
      editDialogVisible: false,
      list: [
        {
          label: "空闲",
          value: 0,
        },
        {
          label: "工作中",
          value: 1,
        },
      ],
      driveList: [],
      dialogVisible: false,
      record: {},
      editRecord: {},
      detailRecord: {},
      disabled: false,
      status: "空闲",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      carList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        departDate: [],
        pickDate: [],
        transportLine: "",
        supplier: "",
        status: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      line: [],
    };
  },
  components: {
    AddPlane,
    AssignPlan,
    EditPlan,
    PlanDetail,
  },
  created() {
    this.getList();
    listAll().then((res) => {
      for (let i in res.rows) {
        this.line.push({
          label: res.rows[i].lineName,
          value: res.rows[i].id,
        });
      }
    });
    getSupplier().then((res) => {
      for (let i in res.data) {
        this.supplierList.push({
          label: res.data[i].name,
          value: res.data[i].id,
        });
      }
    });
  },
  watch: {
    "queryParams.departDate"(newVal) {
      // console.log(newVal);
      if (newVal == null) {
        this.queryParams.departDate = [];
      }
    },
    "queryParams.pickDate"(newVal) {
      // console.log(newVal);
      if (newVal == null) {
        this.queryParams.pickDate = [];
      }
    },
  },
  methods: {
    /** 计划详情 */
    handleDetail(record) {
      this.detailRecord = {
        dialogVisible: true,
        id: record.id,
      };
    },
    /** 编辑计划 */
    handleEdit(record) {
      this.editRecord = {
        dialogVisible: true,
        id: record.id,
      };
    },
    /** 分配计划 */
    assignPlan(row) {
      this.record = {
        title: "分配计划",
        id: row.id,
        dialogVisible: true,
      };
    },
    /** 刷新 */
    refresh() {
      this.dialogVisible = false;
      this.record.dialogVisible = false;
      this.editRecord.dialogVisible = false;
      this.detailRecord.dialogVisible = false;
      this.getList();
    },
    /** 获取计划列表 */
    getList() {
      this.loading = true;
      let data = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        departStartDate: this.queryParams.departDate
          ? this.queryParams.departDate[0]
          : "",
        departEndDate: this.queryParams.departDate ? this.queryParams.departDate[1] : "",
        pickStartDate: this.queryParams.pickDate ? this.queryParams.pickDate[0] : "",
        pickEndDate: this.queryParams.pickDate ? this.queryParams.pickDate[1] : "",
        transportLine: this.queryParams.transportLine,
        supplier: this.queryParams.supplier,
        status: this.queryParams.status,
      };
      getList(data).then((response) => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 删除计划 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除运输计划编号为"' + ids + '"的数据项？')
        .then(function () {
          return deletePlan(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        departDate: [],
        pickDate: [],
        transportLine: "",
        supplier: "",
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
      this.title = "添加运输计划";
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/tms/transportPlan/export",
        {
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,
          departStartDate: this.queryParams.departDate[0],
          departEndDate: this.queryParams.departDate[1],
          pickStartDate: this.queryParams.pickDate[0],
          pickEndDate: this.queryParams.pickDate[1],
          transportLine: this.queryParams.transportLine,
          supplier: this.queryParams.supplier,
        },
        `plan_${new Date().getTime()}.xlsx`
      );
    },
    // 表单隔行变色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
  },
};
</script>
<style>
.el-dialog__body {
  padding: 0px 20px !important;
}

.el-timeline-item__timestamp {
  font-size: 1em !important;
  margin-bottom: 0px !important;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-table .warning-row {
  background: #f7faff;
}

.el-table .success-row {
  background: #ebf1fb;
}

.el-progress-circle {
  width: 60px !important;
  height: 60px !important;
}

.el-progress__text {
  font-size: 12px !important;
}

.el-timeline-item {
  padding-bottom: 0px !important;
}
</style>
